package form;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.x.helpmeget2.R;

import bean.AddressInfo;
import db.AddressDB;

public class BuyAddress extends Activity {

	private EditText schoolText;
	private EditText nameText;
	private EditText phoneText;
    private EditText shortPhoneText; 
    private EditText dormText;
    private EditText dormNumberText;
    
	private String provinces;
	private AddressInfo myAddress;

	private LinearLayout shengLinear;
	private LinearLayout schoolLinear;
	private LinearLayout nameLinear;
	private LinearLayout phoneLinear;
    private LinearLayout shortPhoneLinear;
    //宿舍楼的布局
    private LinearLayout dormLinear;
    //宿舍号的布局
    private LinearLayout dormNumberLinear;
    
	private TextView shengTextView;
	private TextView schoolTextView;
	private TextView nameTextView;
	private TextView phoneTextView;
    private TextView shortPhoneTextView;
    private TextView dormTextView;
    private TextView dormNumberTextView;
    
	private Button postBtn;
	private AddressInfo addressinfo;
	private CheckBox checkBox;

	private LinearLayout onBackLinear;
	private TextView topTxt;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.my_set_buyaddress);
		
		myAddress = new AddressInfo();
		
		Intent i = getIntent();
		provinces = i.getStringExtra("address");
		Bundle b = i.getBundleExtra("address_id");
		
		topTxt = (TextView) this.findViewById(R.id.txt_linear);
		onBackLinear = (LinearLayout) this.findViewById(R.id.download_layout1);
		topTxt.setText("添加地址");

		shengTextView = (TextView) this.findViewById(R.id.my_set_buyaddress_sheng);
		schoolText = (EditText) this.findViewById(R.id.my_school);
		nameText = (EditText) this.findViewById(R.id.my_set_buyaddress_name);
		phoneText = (EditText) this.findViewById(R.id.my_set_buyaddress_phone);
		//短号的编辑框
        shortPhoneText=(EditText)this.findViewById(R.id.my_short_phone);
        //宿舍楼的编辑框
        dormText=(EditText)this.findViewById(R.id.my_set_dorm);
        //宿舍号的编辑框
        dormNumberText=(EditText)this.findViewById(R.id.my_set_dorm_number);
		shengLinear = (LinearLayout) this
				.findViewById(R.id.my_set_buyaddress_sheng_linear);
		schoolLinear = (LinearLayout) this
				.findViewById(R.id.my_school_linear);
		nameLinear = (LinearLayout) this
				.findViewById(R.id.my_set_buyaddress_name_linear);
		phoneLinear = (LinearLayout) this
				.findViewById(R.id.my_set_buyaddress_phone_linear);
		shortPhoneLinear=(LinearLayout)this.findViewById(R.id.my_short_phone_linear);
		dormLinear=(LinearLayout)this.findViewById(R.id.my_set_dorm_linear);
		dormNumberLinear=(LinearLayout)this.findViewById(R.id.my_set_dorm_number_linear);
		schoolTextView = (TextView) this
				.findViewById(R.id.my_school_text);
		nameTextView = (TextView) this
				.findViewById(R.id.my_set_buyaddress_name_text);
		phoneTextView = (TextView) this
				.findViewById(R.id.my_set_buyaddress_phone_text);
		shortPhoneTextView=(TextView) this
				.findViewById(R.id.my_short_phone_text);
		dormTextView=(TextView) this
				.findViewById(R.id.my_set_dorm_text);
		dormNumberTextView=(TextView) this
				.findViewById(R.id.my_set_dorm_number_text);
		checkBox = (CheckBox) this.findViewById(R.id.my_set_address_checkbox);

		postBtn = (Button) this
				.findViewById(R.id.my_set_buyaddress_address_btn);

		if (provinces == null) {
		} else {
			myAddress.setProvinces(provinces);
			shengTextView.setText(provinces);
		}
		
		shengLinear.setOnClickListener(click);
		schoolLinear.setOnClickListener(click);
		nameLinear.setOnClickListener(click);
		phoneLinear.setOnClickListener(click);
		shortPhoneLinear.setOnClickListener(click);
		dormLinear.setOnClickListener(click);
		dormNumberLinear.setOnClickListener(click);
		postBtn.setOnClickListener(click);
		onBackLinear.setOnClickListener(click);

		schoolText.setOnFocusChangeListener(focusChanger);
		nameText.setOnFocusChangeListener(focusChanger);
		phoneText.setOnFocusChangeListener(focusChanger);
		shortPhoneText.setOnFocusChangeListener(focusChanger);
		dormText.setOnFocusChangeListener(focusChanger);
		dormNumberText.setOnFocusChangeListener(focusChanger);
		
		if (b != null) {
			addressinfo = (AddressInfo) b.get("address");

			phoneText.setText(addressinfo.getPhone());
			shengTextView.setText(addressinfo.getProvinces());
			schoolText.setText(addressinfo.getSchool());
			nameText.setText(addressinfo.getName());
			shortPhoneText.setText(addressinfo.getShortPhone()); 
		    dormText.setText(addressinfo.getDorm());
		    dormNumberText.setText(addressinfo.getDormNumber());

			phoneText.setVisibility(View.GONE);
			phoneLinear.setVisibility(View.VISIBLE);
			phoneTextView.setText(addressinfo.getPhone());
			
			schoolText.setVisibility(View.GONE);
			schoolLinear.setVisibility(View.VISIBLE);
			schoolTextView.setText(addressinfo.getSchool());

			nameText.setVisibility(View.GONE);
			nameLinear.setVisibility(View.VISIBLE);
			nameTextView.setText(addressinfo.getName());
			
			shortPhoneText.setVisibility(View.GONE);
			shortPhoneLinear.setVisibility(View.VISIBLE);
			shortPhoneTextView.setText(addressinfo.getShortPhone());
			
			dormText.setVisibility(View.GONE);
			dormLinear.setVisibility(View.VISIBLE);
			dormTextView.setText(addressinfo.getDorm());
			
			dormNumberText.setVisibility(View.GONE);
			dormNumberLinear.setVisibility(View.VISIBLE);
			dormNumberTextView.setText(addressinfo.getDormNumber());
			
			if(addressinfo.isStatus()){
				checkBox.setChecked(true);
			}else{
				checkBox.setChecked(false);
			}
			
			myAddress.setId(addressinfo.getId());
			myAddress.setProvinces(addressinfo.getProvinces());
			myAddress.setSchool(addressinfo.getSchool());
			myAddress.setName(addressinfo.getName());
			myAddress.setPhone(addressinfo.getPhone());
			myAddress.setShortPhone(addressinfo.getShortPhone());
			myAddress.setDorm(addressinfo.getDorm());
			myAddress.setDormNumber(addressinfo.getDormNumber());
			myAddress.setStatus(addressinfo.isStatus());
		}
	}

	OnFocusChangeListener focusChanger = new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			myAddress.setSchool(schoolText.getText().toString());
			myAddress.setName(nameText.getText().toString());
			myAddress.setPhone(phoneText.getText().toString());
            myAddress.setShortPhone(shortPhoneText.getText().toString());
            myAddress.setDorm(dormText.getText().toString());
            myAddress.setDormNumber(dormNumberText.getText().toString());
			switch (v.getId()) {
			case R.id.my_school:
				if (!hasFocus && myAddress.getSchool().length() > 0) {
					schoolText.setVisibility(View.GONE);
					schoolLinear.setVisibility(View.VISIBLE);

					schoolTextView.setText(myAddress.getSchool());
				}

				if (hasFocus) {
					schoolText.setSelectAllOnFocus(true);
				}
				break;
			case R.id.my_set_buyaddress_name:
				if (!hasFocus && myAddress.getName().length() > 0) {
					nameText.setVisibility(View.GONE);
					nameLinear.setVisibility(View.VISIBLE);

					nameTextView.setText(myAddress.getName());
				}

				if (hasFocus) {
					nameText.setSelectAllOnFocus(true);
				}
				break;
			case R.id.my_set_buyaddress_phone:
				if (!hasFocus && myAddress.getPhone().length() > 0) {
					phoneText.setVisibility(View.GONE);
					phoneLinear.setVisibility(View.VISIBLE);

					phoneTextView.setText(myAddress.getPhone());
				}
				if (hasFocus) {
					phoneText.setSelectAllOnFocus(true);
				}
				break;
			case R.id.my_short_phone:
				if (!hasFocus && myAddress.getShortPhone().length() > 0) {
					shortPhoneText.setVisibility(View.GONE);
					shortPhoneLinear.setVisibility(View.VISIBLE);

					shortPhoneTextView.setText(myAddress.getShortPhone());
				}
				if (hasFocus) {
					shortPhoneText.setSelectAllOnFocus(true);
				}
				break;
			case R.id.my_set_dorm:
				if (!hasFocus && myAddress.getDorm().length() > 0) {
					dormText.setVisibility(View.GONE);
					dormLinear.setVisibility(View.VISIBLE);

					dormTextView.setText(myAddress.getDorm());
				}
				if (hasFocus) {
					dormText.setSelectAllOnFocus(true);
				}
				break;
			case R.id.my_set_dorm_number:
				if (!hasFocus && myAddress.getDormNumber().length() > 0) {
					dormNumberText.setVisibility(View.GONE);
					dormNumberLinear.setVisibility(View.VISIBLE);

					dormNumberTextView.setText(myAddress.getDormNumber());
				}
				if (hasFocus) {
					dormNumberText.setSelectAllOnFocus(true);
				}
				break;
			default:
				break;
			}
		}
	};

	OnClickListener click = new OnClickListener() {

		@SuppressLint("SimpleDateFormat") @Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.my_set_buyaddress_sheng_linear:
				Intent i = new Intent(BuyAddress.this, AddressChoose.class);
				i.putExtra("Boolean", "aaa");
				startActivity(i);
				break;
			case R.id.my_school_linear:
				schoolText.setVisibility(View.VISIBLE);
				schoolLinear.setVisibility(View.GONE);

				schoolText.setFocusable(true);
				schoolText.setFocusableInTouchMode(true);

				schoolText.requestFocus();
				break;
			case R.id.my_set_buyaddress_name_linear:
				nameText.setVisibility(View.VISIBLE);
				nameLinear.setVisibility(View.GONE);

				nameText.setFocusable(true);
				nameText.setFocusableInTouchMode(true);

				nameText.requestFocus();
				break;
			case R.id.my_set_buyaddress_phone_linear:
				phoneText.setVisibility(View.VISIBLE);
				phoneLinear.setVisibility(View.GONE);

				phoneText.setFocusable(true);
				phoneText.setFocusableInTouchMode(true);

				phoneText.requestFocus();
				break;
			case R.id.my_short_phone_linear:
				shortPhoneText.setVisibility(View.VISIBLE);
				shortPhoneLinear.setVisibility(View.GONE);

				shortPhoneText.setFocusable(true);
				shortPhoneText.setFocusableInTouchMode(true);

				shortPhoneText.requestFocus();
				break;
			case R.id.my_set_dorm_linear:
				dormText.setVisibility(View.VISIBLE);
				dormLinear.setVisibility(View.GONE);

				dormText.setFocusable(true);
				dormText.setFocusableInTouchMode(true);

				dormText.requestFocus();
				break;
			case R.id.my_set_dorm_number_linear:
				dormNumberText.setVisibility(View.VISIBLE);
				dormNumberLinear.setVisibility(View.GONE);

				dormNumberText.setFocusable(true);
				dormNumberText.setFocusableInTouchMode(true);

				dormNumberText.requestFocus();
				break;
			case R.id.my_set_buyaddress_address_btn:
				myAddress.setSchool(schoolText.getText().toString());
				myAddress.setName(nameText.getText().toString());
				myAddress.setPhone(phoneText.getText().toString());
				
				if (myAddress.getPhone().length() > 0) {
					phoneText.setVisibility(View.GONE);
					phoneLinear.setVisibility(View.VISIBLE);
					phoneTextView.setText(myAddress.getPhone());
				}
				postBtn.requestFocus();

				postBtn.setFocusable(true);
				postBtn.setFocusableInTouchMode(true);

				if (myAddress.getProvinces().length() < 1 || myAddress.getSchool().length() < 1
						|| myAddress.getName().length() < 1 || myAddress.getPhone().length() < 1
						||myAddress.getShortPhone().length()<1||myAddress.getDorm().length()<1||
						myAddress.getDormNumber().length()<1) {
					Toast.makeText(getBaseContext(), "请完整填写收货人资料", Toast.LENGTH_SHORT).show();
					return;
				}
				
				myAddress.setStatus(checkBox.isChecked());
				AddressDB addressDB = AddressDB.getInstance(getBaseContext());
				
				if(checkBox.isChecked()){
					List<AddressInfo> list = addressDB.queryAddress();
					if(list!=null){
						Iterator<AddressInfo> iterator = list.iterator();
						while(iterator.hasNext()){
							AddressInfo a = iterator.next();
							a.setStatus(false);
							addressDB.updeteAddress(a);
						}
					}
					
				}
				
				
				if (addressinfo != null) {
					if(addressDB.updeteAddress(myAddress)){
						Toast.makeText(getBaseContext(), "修改地址成功", Toast.LENGTH_LONG).show();
					}else{
						Toast.makeText(getBaseContext(), "修改地址失败", Toast.LENGTH_LONG).show();
					}
				} else {
					
					SimpleDateFormat format = new SimpleDateFormat(
							"yyyyMMddHHmmss");
					Date date = new Date();
					String id = format.format(date);
					myAddress.setId(id);
					
					if(addressDB.insertAddress(myAddress)){
						Toast.makeText(getBaseContext(), "添加地址成功", Toast.LENGTH_LONG).show();
					}else{
						Toast.makeText(getBaseContext(), "添加地址失败", Toast.LENGTH_LONG).show();
					}
				}
				
					Intent intent = new Intent(BuyAddress.this, PersonAddress.class);
					startActivity(intent);
					finish();
				
				
				break;
				
			case R.id.download_layout1:
				finish();
				break;
			default:
				break;
			}
		}
	};

}
